package com.hyp.pay.UI.Home.presenter;

import android.util.Log;

import com.hyp.pay.UI.Home.presenter.contract.EditContract;
import com.hyp.pay.base.RxPresenter;
import com.hyp.pay.http.Constants;
import com.hyp.pay.http.RetrofitHelper;
import com.hyp.pay.model.FindRecharge;
import com.hyp.pay.model.UserJson;

import org.json.JSONException;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.subscribers.ResourceSubscriber;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/6/2.
 */

public class EditPresenter extends RxPresenter<EditContract.View> implements EditContract.Presenter{

    private RetrofitHelper mRetrofitHelper;

    @Inject
    public EditPresenter(RetrofitHelper mRetrofitHelper) {
        this.mRetrofitHelper = mRetrofitHelper;
    }



    @Override
    public void getUploadFileIDCard(String filePath, String filePath2, String filePath3,
                                    String filePath4, String filePath5, int userid, String pay) {
        File file = new File(filePath);
        File file2 = new File(filePath2);
        File file3 = new File(filePath3);
        File file4 = new File(filePath4);
        File file5 = new File(filePath5);

        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
        RequestBody requestFile2 = RequestBody.create(MediaType.parse("image/*"), file2);
        RequestBody requestFile3 = RequestBody.create(MediaType.parse("image/*"), file3);
        RequestBody requestFile4 = RequestBody.create(MediaType.parse("image/*"), file4);
        RequestBody requestFile5 = RequestBody.create(MediaType.parse("image/*"), file5);


        MultipartBody.Part body =
                MultipartBody.Part.createFormData("SignFace", file.getName(), requestFile);
        MultipartBody.Part body2 =
                MultipartBody.Part.createFormData("SignDraweeDiv", file2.getName(), requestFile2);
        MultipartBody.Part body3 =
                MultipartBody.Part.createFormData("SignPayerDiv", file3.getName(), requestFile3);
        MultipartBody.Part body4 =
                MultipartBody.Part.createFormData("SignDraweeName", file4.getName(), requestFile4);
        MultipartBody.Part body5 =
                MultipartBody.Part.createFormData("SignPayerName", file5.getName(), requestFile5);


        Flowable<String> flowable = mRetrofitHelper.UploadFile(body,body2,body3,body4,body5,userid,pay);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUplod(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }

    @Override
    public void getFindRecharge(String charge) {
        Flowable<FindRecharge> flowable = mRetrofitHelper.findRecharge(charge);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<FindRecharge>() {

            @Override
            public void onNext(FindRecharge data) {

                Log.i("olj",data.toString());

                if (getView() != null) {
                    try {
                        getView().showFindRecharge(data);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }

    @Override
    public void getUsr(String userid) {
        Flowable<UserJson> flowable = mRetrofitHelper.findUser(userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<UserJson>() {

            @Override
            public void onNext(UserJson data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUser(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }
}
